package com.ruoyi.jksd.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.jksd.domain.XyUsers;
import com.ruoyi.jksd.service.IXyUsersService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jksd.domain.XyBalanceLog;
import com.ruoyi.jksd.service.IXyBalanceLogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员-收支明细Controller
 *
 * @author ruoyi
 * @date 2024-05-26
 */
@RestController
@RequestMapping("/balancelog/balancelog")
public class XyBalanceLogController extends BaseController {
    @Autowired
    private IXyBalanceLogService xyBalanceLogService;

    @Autowired
    private IXyUsersService xyUsersService;

    /**
     * 查询会员-收支明细列表
     */
    @GetMapping("/list")
    public TableDataInfo list(XyBalanceLog xyBalanceLog) {
        startPage();
        List<XyBalanceLog> list = xyBalanceLogService.selectXyBalanceLogList(xyBalanceLog);
        return getDataTable(list);
    }

    /**
     * 导出会员-收支明细列表
     */
    @PreAuthorize("@ss.hasPermi('balancelog:balancelog:export')")
    @Log(title = "会员-收支明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XyBalanceLog xyBalanceLog) {
        List<XyBalanceLog> list = xyBalanceLogService.selectXyBalanceLogList(xyBalanceLog);
        ExcelUtil<XyBalanceLog> util = new ExcelUtil<XyBalanceLog>(XyBalanceLog.class);
        util.exportExcel(response, list, "会员-收支明细数据");
    }

    /**
     * 获取会员-收支明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('balancelog:balancelog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(xyBalanceLogService.selectXyBalanceLogById(id));
    }


    /**
     * 新增会员-收支明细
     */
    @PreAuthorize("@ss.hasPermi('balancelog:balancelog:add')")
    @Log(title = "会员-收支明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XyBalanceLog xyBalanceLog) {
        return toAjax(xyBalanceLogService.insertXyBalanceLog(xyBalanceLog));
    }

    /**
     * 修改会员-收支明细
     */
//    @PreAuthorize("@ss.hasPermi('balancelog:balancelog:edit')")
    @Log(title = "会员-收支明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XyBalanceLog xyBalanceLog) {
        return toAjax(xyBalanceLogService.updateXyBalanceLog(xyBalanceLog));
    }

    /**
     * 删除会员-收支明细
     */
//    @PreAuthorize("@ss.hasPermi('balancelog:balancelog:remove')")
    @Log(title = "会员-收支明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(xyBalanceLogService.deleteXyBalanceLogByIds(ids));
    }


    /**
     * 获取会员-收支明细详细信息
     */
//    @PreAuthorize("@ss.hasPermi('balancelog:balancelog:query')")
    @GetMapping(value = "/countBalance")
    public AjaxResult countBalance() {
        String userName = getUsername();
        XyUsers xyUsers = new XyUsers();
        xyUsers.setUsername(userName);
        List<XyUsers> xyUsersList = xyUsersService.selectXyUsersList(xyUsers);
        if (CollectionUtils.isNotEmpty(xyUsersList)) {
            xyUsers = xyUsersList.get(0);
        }
        return success(xyBalanceLogService.countBalance(xyUsers.getId()));
    }
}
